/**
 * Beaver - Zend/Ext.Direct framework
 *
 * @license     http://www.gnu.org/licenses/gpl-2.0.txt
 * @author      Mihály Kuprivecz <mkuprivecz@gmail.com>
 * @namespace   Beaver.Modules.User
 */

Ext.ns('Beaver.Modules.City');

Beaver.Modules.City.Store = Ext.extend( Beaver.Modules.General.Store, {
	constructor: function(config) {
		config        = config || {};
		config.fields = config.fields || [];
		config.controllerName = 'City';
		config.autoLoad = config.autoLoad || {params:{start:0, limit:500}};
		config.remoteSort = config.remoteSort || true;
		
		Ext.applyIf( config.fields, [
			{ name: 'id', mapping: 'id' },
			{ name: 'country_id', mapping: 'country_id', allowBlank: false },
			{ name: 'name', mapping: 'name', allowBlank: false },
		]);
		
		Beaver.Modules.City.Store.superclass.constructor.apply(this, [config]);
	}
});

Ext.reg('beavercity', Beaver.Modules.City.Store);


new Beaver.Modules.City.Store({
	autoLoad: {
		params:{
			sort: 'name',
			dir: 'ASC'
		}
	},
	remoteSort: false,
	storeId: 'cityStore'
})

Beaver.Modules.City.Grid = Ext.extend(Beaver.Modules.General.Grid, {
	initComponent: function() {
		var store  = new Beaver.Modules.City.Store();
		var countryStore = new Beaver.Modules.Country.Store({
			sortInfo: {
				field: 'name_hu',
				direction: 'ASC'
			}
		});
		var editor = new Beaver.Modules.General.RowEditor()
		editor.on('canceledit', function() {
			if(store.getAt(0).phantom)
			{
				store.removeAt(0);
			}
		});
		editor.on('beforeedit', function() {
			console.log(this);
		});
		
		var countryComboBox = new Ext.form.ComboBox({
			typeAhead: true,
			//editable: false,
			forceSelection: true,
			allowBlank: false,
			mode: 'remote',
			store : countryStore,
			valueField: 'id',
			displayField: 'name_hu'
		});
		
		var self  = this;
		var config = {
			store: store,
			plugins: [ editor ],
			columns: [
				{
					header: 'Országkód',
					width: 20,
					sortable: true,
					dataIndex: 'country_id',
					renderer: function(value, metaData, record, rowIndex, colIndex, store) {
						if(typeof(value)!="undefined") return countryStore.getAt(countryStore.find("id",value)).data.name_hu;
					},
					editor: countryComboBox
				},
				{
					header: 'Név',
					sortable: true,
					dataIndex: 'name',
					editor: new Ext.form.TextField()
				}

			],
			tbar: [
				{
					text: 'Település hozzáadása',
					handler: function(){

						if(self.store.getAt(0) && self.store.getAt(0).phantom)
						{
							self.store.removeAt(0);
						}
						var record = new store.recordType({
							country_id: 'HU',
							name : ''
						});

						editor.stopEditing();
						self.store.insert(0, record);
						editor.startEditing(0);
					}
				},
				{
					text: 'Kijelöltek törlése',
					handler: function(){
						
						editor.stopEditing();
						var records = self.getSelectionModel().getSelections();
						if (records.length > 0)
						{
					
							Ext.Msg.confirm('Törélés', 'Biztosan törölni szeretné az összes kijelölt elemet?', function( answer ){
								if (answer == "yes")
								{
									self.getStore().remove(records);
								}
				        	},this);
						}
					}
				}
			],
			bbar: new Ext.PagingToolbar({
				store: store,
				pageSize:500,
				displayInfo:true
			})


		};
		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);
		Beaver.Modules.City.Grid.superclass.initComponent.apply(this, arguments);
	},
	onRender:function() {
		Beaver.Modules.City.Grid.superclass.onRender.apply(this, arguments);
	}
});

Ext.reg('beavergridcity', Beaver.Modules.City.Grid);